package com.credithc.signature.core.util;

import lombok.Getter;

import java.util.ArrayList;

/**
 * @author zhanghanqiu181212@credithc.com
 * @Date 2020/12/21 14:30
 */

public class FixedLengthStack<T> {
    @Getter
    public String name;
    public Integer maxLength;
    @Getter
    private Long maxTime = Long.MIN_VALUE;
    @Getter
    private Long minTime = Long.MAX_VALUE;


    public Integer top = -1;
    private ArrayList<T> arrayList = new ArrayList();

    public FixedLengthStack(Integer maxLength, String name) {
        this.maxLength = maxLength;
        this.name = name;
    }

    public boolean isEmpty() {
        if (arrayList.size() - 1 != top) {
            top = arrayList.size() - 1;
        }
        return top == -1;
    }

    public boolean isFull() {
        if (arrayList.size() - 1 != top) {
            top = arrayList.size() - 1;
        }
        return top == maxLength - 1;
    }


    public void add(T t) {
        if (isFull()) {
            remove();
        }
        try {
            Long tn = Long.valueOf((String) t);
            if (tn > maxTime) {
                maxTime = tn;
            }
            if (tn < minTime) {
                minTime = tn;
            }
        } catch (NumberFormatException e) {

        }
        arrayList.add(t);
        top += 1;
    }

    public void remove() {
        arrayList.remove(0);
        top -= 1;
    }

    public ArrayList<T> getAll() {
        return (ArrayList) arrayList.clone();
    }


    public static void main(String[] args) {

    }

}
